// JavaScript Document

// 浏览器检测
(function () {
    window.sys = {};
    var ua = navigator.userAgent.toLowerCase();
    var s;
    
    (s = ua.match(/msie ([\d.]+)/)) ? sys.ie = s[1] :
    (s = ua.match(/firefox\/([\d.]+)/)) ? sys.firefox = s[1] :
    (s = ua.match(/chrome\/([\d.]+)/)) ? sys.chrome = s[1] : 
    (s = ua.match(/opera\/.*version\/([\d.]+)/)) ? sys.opera = s[1] :
    (s = ua.match(/version\/([\d.]+).*safari/)) ? sys.safari = s[1] : 0;
    
    if (/webkit/.test(ua)) {
        sys.webkit = ua.match(/webkit\/([\d.]+)/)[1];
    }
})();

// DOM加载完毕即执行，区别于onload
function addDomLoaded(fn) {
    var isReady = false;
    var timer = null;
    
    function doReady() {
        if (timer) {
            clearInterval(timer);
        }
        if (isReady) {
            return;
        }
        isReady = true;
        fn();
    }
    
    if ((sys.opera && sys.opera < 9) || (sys.firefox && sys.firefox < 3) || (sys.webkit && sys.webkit < 525)) {
        // 无论采用哪种，基本用不上
        /*
        timer = setInterval(function () {
            if (/loaded|complete/.test(document, readyState)) {
                // loaded是部分加载，有可能只是DOM加载完毕,complete是完全加载，类似于onload
                doReady();
            }
        }, 1);
        */
        timer = setInterval(function () {
            if (document && document.getElementById && document.getElementsByTagName && document.body) {
                doReady();
            }
        }, 1);
    } 
    else if (document.addEventListener) {
		addEvent(document, 'DOMContentLoaded', function () {
			//doReady();
			fn();
			removeEvent(document, 'DOMContentLoaded', arguments.callee);
		});
    } 
    else if (sys.ie && sys.ie < 9) {
        var timer = null;
        timer = setInterval(function () {
            try {
                document.documentElement.doScroll('left');
                doReady();
            } catch (e) {
            }
        }, 1);
    }
}

// 跨浏览器添加事件绑定
function addEvent(obj, type, fn) {
    if (typeof obj.addEventListener != 'undefined') {
        obj.addEventListener(type, fn, false);
    } else {
        // 创建一个存放事件的哈希表
        if (!obj.events) {
            obj.events = {};
        }
        // 第一次执行时执行
        if (!obj.events[type]) {
            // 创建一个存放事件处理函数的数组
            obj.events[type] = [];
            // 把第一次的事件处理函数先存储到第一个位置上
            if (obj['on' + type]) {
                obj.events[type][0] = fn;
            }
        } /* End if (!obj.events) */
        else {
            // 同一个注册函数进行屏蔽，不添加到计时器中
            if (addEvent.equal(obj.events[type], fn)) {
                return false;
            }
        }
        // 第二次开始用事件计数器存储
        // 同一类型的事件执行不同函数则添加到计数器中，区别于上面的屏蔽事件
        obj.events[type][addEvent.ID++] = fn;
        // 执行事件处理函数
        obj['on' + type] = addEvent.exec;
    }
}

// 为每个事件分配一个计数器
addEvent.ID = 1;

// 执行事件处理函数
addEvent.exec = function (event) {
    var e = event || addEvent.fixEvent(window.event);
    var es = this.events[e.type];
    for (var i in es) {
        es[i].call(this, e);
    }
};

// 对同一个注册函数进行屏蔽
addEvent.equal = function (es, fn) {
    for (var i in es) {
        if (es[i] == fn) {
            return true;
        }
    }
    return false;
};

// 把IE常用的Event对象配对到W3C中去
addEvent.fixEvent = function (event) {
    event.preventDefault = addEvent.fixEvent.preventDefault;
    event.stopPropagation = addEvent.fixEvent.stopPropagation;
    event.target = event.srcElement;
    return event;
};

// IE阻止默认行为
addEvent.fixEvent.preventDefault = function () {
    this.returnValue = false;
};

// IE取消冒泡
addEvent.fixEvent.stopPropagation = function () {
    this.cancelBubble = true;
};

// 跨浏览器删除事件
function removeEvent(obj, type, fn) {
    if (typeof obj.removeEventListener != 'undefined') {
        obj.removeEventListener(type, fn, false);
    } else {
        if (obj.events) {
            for (var i in obj.events[type]) {
                if (obj.events[type][i] == fn) {
                    delete obj.events[type][i];
                }
            }
        }
    }
}

// 跨浏览器获取视口大小
function getInner() {
    if (typeof window.innerWidth != 'undefined') {
        return {
            width : window.innerWidth,
            height : window.innerHeight
        };
    }
}

// 跨浏览器获取滚动条位置
function getScroll() {
    return {
        top : document.documentElement.scrollTop || document.body.scrollTop,
        left : document.documentElement.scrollLeft || document.body.scrollLeft
    };
}

// 跨浏览器获取style
function getStyle(element, attr) {
    var value;
    if (typeof window.getComputedStyle != 'undefined') {        //W3C
        value = window.getComputedStyle(element, null)[attr];
    } else if (typeof element.currentStyle != 'undefined') {    //IE
        value = element.currentStyle[attr];
    }
    return value;
}

// 判断class是否存在
function hasClass(element, className) {
    return element.className.match(new RegExp('(\\s|^)' + classsName + '(\\s|$)'));
}

// 跨浏览器添加link规则
function insertRule(sheet, selectorText, cssText, position) {
    if (typeof sheet.insertRule != 'undefined') {           // W3C
        sheet.insertRule(selectorText + '{' + cssText + '}', position);
    } else if (typeof sheet.addRule != 'undefined') {       // IE
        sheet.addRule(selectorText, cssText, position);
    }
}

// 跨浏览器移除link规则
function deleteRule(sheet, index) {
    if (typeof sheet.deleteRule != 'undefined') {           // W3C
        sheet.deleteRule(index);
    } else if (typeof sheet.removeRule != 'undefined') {    // IE
        sheet.removeRule(index);
    }
}

// 跨浏览器获取innerText
function getInnerText(element) {
    return (typeof element.textContent == 'string') ? element.textContent : element.innerText;
}


// 跨浏览器设置innerText
function setInnerText(element, text) {
    if (typeof element.textContent == 'string') {
        element.textContent = text;
    } else {
        element.innerText = text;
    }
}

// 获取某一个元素最外层顶点的位置
function offsetTop(element) {
    var top = element.offsetTop;
    var parent = element.offsetParent;
    while (parent != null) {
        top += parent.offsetParent;
        parent = parent.offsetParent;
    }
    return top;
}

// 删除前后空格
function trim(str) {
    return str.replace(/(^\s*)|(\s*$)/g, '');
}

// 某一个值是否存在某一个数组中
function inArray(array, value) {
    for (var i in array) {
        if (array[i] == value) {
            return true;
        }
    }
    return false;
}

// 获取某一个节点的上一个节点的索引
function prevIndex(current, parent) {
    var length = parent.children.length;
    if (curren == 0) {
        // 当前节点为第一个时返回最后一个节点的索引
        return length - 1;
    }
    return parseInt(curren) - 1;
}

// 获取某一个节点的下一个节点的索引
function nextIndex(curren, parent) {
    var length = parent.children.length;
    if (curren == length - 1) {
        return 0;
    }
    return parseInt(curren) + 1;
}

// 滚动条固定
function fixedScroll() {
    window.scrollTo(fixedScroll.left, fixedScroll.top);
}

// 阻止默认行为
function predef(e) {
    e.preventDefault();
}

// 创建cookie
function setCookie(name, value, expires, path, domain, secure) {
    var cookieText = encodeURIComponent(name) + '=' + encodeURIComponent(value);
    
    if (expires instanceof Date) {
        cookieText += ';expires=' + expries;
    }
    if (path) {
        cookieText += ';path=' + path;
    }
    if (domain) {
        cookie += ';domain=' + domain;
    }
    if (secure) {
        cookieText += ';secure';
    }
    document.cookie = cookieText;
}

// 获取cookie
function getCookie(name) {
    var cookieName = encodeURIComponent(name) + '=';
    var cookieStart = document.cookie.indexOf(cookieName);
    var cookieValue = null;
    
    if (cookieStart > -1) {
        var cookieEnd = document.cookie.indexOf(';', cookieStart);
        if (cookieStart == -1) {
            cookieEnd = document.cookie.length;
        }
        cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd));
    }
    return cookieValue;
}

// 删除cookie
function unsetCookie(name) {
    document.cookie = name + '=;expires=' + new Date(0);
}

// 获取鼠标坐标
function getMousePos(event) {
    var e = event || window.event;
    var scrollX = getScroll().left;
    var scrollY = getScroll().top;
    var x = e.pageX || e.clientX + scrollX;
    var y = e.pageY || e.clientY + scrollY;
    this.x = x;
    this.y = y;
    return this;
}
